package com.pmcc.core.service;

import com.pmcc.core.entity.security.Employee;
import com.pmcc.core.entity.security.Role;
import com.pmcc.core.repository.RoleRepository;
import com.pmcc.utils.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by Tomas on 2018/4/8.
 */
@Service
@Transactional
public class RoleService {
    @Autowired
    RoleRepository roleRepository;

    public ResultBean showLog(AntdPage antdPage , Role model) throws Exception{
        ResultBean resultBean=new ResultBean();
        Page page=roleRepository.findAll(this.getSpecification(model),antdPage.getPageRequest());

        resultBean.setPage(antdPage.getDefaultCurrent());
        resultBean.setTotal(page.getTotalElements());
        resultBean.setResultCode(ResultBean.SUCCESS);
        resultBean.setPageSize(antdPage.getPageSize());
        resultBean.setRows(page.getContent());
        return resultBean;
    }

    public Specification<Role> getSpecification(Role model) {
        return new Specification<Role>() {
            @Override
            public Predicate toPredicate(Root<Role> root, CriteriaQuery<?> query, CriteriaBuilder builder) {
                List<Predicate> list = new ArrayList<Predicate>();

                list.add(builder.equal(root.get("delFlag").as(Integer.class),0));
                Predicate[] p = new Predicate[list.size()];
                return builder.and(list.toArray(p));
            }
        };
    }

    /**
     * 新增
     * @param remark
     *@param name
     * @param code
     * @param str
     * @param str1
     * @return
     */
    public ResultBean add(String remark,String name,String code,String str,String str1){
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try{
            Role role=new Role();
            role.setRemark(remark);
            role.setName(name);
            role.setCode(code);
            role.setMenuKey(str);
            role.setState(0);
            role.setMenuList(str1);
            role.setCreateTime(new Date());
            role.setCreater(OnlineUser.getName());
            role.setDelFlag(0);
            roleRepository.saveAndFlush(role);
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setResultDesc("新增成功");
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }



    /**
     * 修改
     * @param map
     * @return
     */
    public ResultBean update(Map map) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Role c = roleRepository.findById(map.get("id").toString());
            BeanUtils.setBeanData(c,map);
            roleRepository.saveAndFlush(c);
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setObject(c);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public ResultBean deleteById(String id,Integer flag) {
        ResultBean resultBean = new ResultBean(ResultBean.SUCCESS);
        try {
            Role f = roleRepository.findById(id);
            f.setDelFlag(flag);
            Role role=roleRepository.saveAndFlush(f);
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setObject(role);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

    /**
     * 查询角色
     * @param id
     * @return
     */
    public ResultBean findById(String id){
        ResultBean resultBean = new ResultBean();
        try{
            Role role=roleRepository.findById(id);
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setObject(role);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }
    /**
     * 查询所有角色
     */
    public ResultBean findAll(){
        ResultBean resultBean = new ResultBean();
        try{
            List<Role> role=roleRepository.findAll();
            resultBean.setResultCode(ResultBean.SUCCESS);
            resultBean.setRows(role);
        }catch (Exception e){
            resultBean.setResultCode(ResultBean.FAIL);
        }
        return resultBean;
    }

}
